﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Plane
{
    public partial class Shop : Form
    {
        public (string Password, int Money, int HighestScore, int RecoveryAmount, int BulletAmount, string PlaneSkin, string KeyUp, string KeyDown, string KeyLeft, string KeyRight, string KeyFire, int SoundVolume) User;
        public string username;

        public Shop((string Password, int Money, int HighestScore, int RecoveryAmount, int BulletAmount, string PlaneSkin, string KeyUp, string KeyDown, string KeyLeft, string KeyRight, string KeyFire, int SoundVolume) User, string username)
        {
            InitializeComponent();
            textBox_money.Text = "金币数："+User.Money.ToString();
            this.User = User;
            this.username = username;
        }

        private void button_return_Click(object sender, EventArgs e)
        {
            MainWindow mainwindow = new MainWindow(username);
            mainwindow.Show();
            this.Close();
        }

        private void button_buy1_Click(object sender, EventArgs e)
        {
            //用字符存储了是否购买的信息，字符为1就是买了，0就是没买
            char isBuy = User.PlaneSkin[1];
            if (isBuy == '0')
            {
                if (User.Money >= 5000)
                {
                    User.Money -= 5000;
                    User.PlaneSkin = string.Concat(User.PlaneSkin[0],'1',User.PlaneSkin[2]); //更新拥有的飞机皮肤，Concat用于字符拼接
                    textBox_money.Text = "金币数：" + User.Money.ToString(); //更新商店界面金币数
                    UserDbHelper.UpdateMoney(username, User.Money); //更新数据库金币数
                    UserDbHelper.UpdatePlaneSkin(username, User.PlaneSkin);//更新数据库飞机皮肤
                    MessageBox.Show("购买成功！", "交易完成",
               MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("金币数量不足！需要5000金币", "交易失败",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("您已经拥有该皮肤啦！", "提示", 
             MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button_buy2_Click(object sender, EventArgs e)
        {
            char isBuy = User.PlaneSkin[2];
            if (isBuy == '0')
            {
                if (User.Money >= 5000)
                {
                    User.Money -= 5000;
                    User.PlaneSkin = string.Concat(User.PlaneSkin[0],User.PlaneSkin[1],'1'); //更新拥有的飞机皮肤，Concat用于字符拼接
                    textBox_money.Text = "金币数：" + User.Money.ToString(); //更新商店界面金币数
                    UserDbHelper.UpdateMoney(username, User.Money); //更新数据库金币数
                    UserDbHelper.UpdatePlaneSkin(username, User.PlaneSkin);//更新数据库飞机皮肤
                    MessageBox.Show("购买成功！", "交易完成",
               MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("金币数量不足！需要5000金币", "交易失败",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("您已经拥有该皮肤啦！", "提示",
             MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button_add_recoveryamount_Click(object sender, EventArgs e)
        {
            if (User.Money >= 1000)
            {
                User.Money -= 1000;
                User.RecoveryAmount += 5; //更新恢复量
                textBox_money.Text = "金币数：" + User.Money.ToString(); //更新商店界面金币数
                UserDbHelper.UpdateMoney(username, User.Money); //更新数据库金币数
                UserDbHelper.UpdateRecoveryAmount(username,User.RecoveryAmount);//更新数据库恢复量
                MessageBox.Show("升级成功！", "交易完成",
           MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("金币数量不足！需要1000金币", "交易失败",
           MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button_bullet_amount_Click(object sender, EventArgs e)
        {
            if(User.BulletAmount == 2)
            {
                MessageBox.Show("子弹数量已达上限", "交易失败",
           MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (User.Money >= 1000)
                {
                    User.Money -= 1000;
                    User.BulletAmount += 1; //更新子弹数量
                    textBox_money.Text = "金币数：" + User.Money.ToString(); //更新商店界面金币数
                    UserDbHelper.UpdateMoney(username, User.Money); //更新数据库金币数
                    UserDbHelper.UpdateBulletAmount(username,User.BulletAmount);//更新数据库子弹数
                    MessageBox.Show("升级成功！", "交易完成",
               MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("金币数量不足！需要1000金币", "交易失败",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
